<?php
require_once("scripts/constraint.php");
require_once("scripts/EKUser.php");
require_once("scripts/EKEvaluation.php");

session_start();

function checkAdmin()
{
	if(!isset($_SESSION['userid']) || $_SESSION['userid']=="")
	{
		return false;
	}

	$thisUser = new EKUser($_SESSION['userid']);
	if(!$thisUser)
	{
		return false;
	}
	
	if($thisUser->getType() != 3)
	{
		return false;
	}
		
	return true;
}

# when the user in the current session is admin, this also get the disabled user
$allUser = EKUser::getAllNormalUsersSortedByExp(checkAdmin());


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>EKA User List</title>
</head>
<body>
<?php
if(($allUser != 0) && count($allUser)>0){
	echo "<h3>User list</h3>";
	if(checkAdmin())
	{
		
		echo "<table><th width='10%'>Rank</th><th width='10%'>Name</th><th width='10%'>Level</th><th width='10%'>Exp</th><th width='10%'>Informational Rating</th><th width='10%'>Structural Rating</th>";
		echo "<th width='20%'>Enabled</th><th width='20%'>Type</th>";
		for($i=0;$i<count($allUser);$i++){
			
			if($allUser[$i]->getEnabled())
			{
				$enableLink = "<a href='user_admin.php?userid=".$allUser[$i]->getId()."&action=disable'>Disabled</a>";
			}else{
				$enableLink = "<a href='user_admin.php?userid=".$allUser[$i]->getId()."&action=enable'>Enabled</a>";
			}
			if($allUser[$i]->getType()==0)
			{
				$typeLink = "<a href='user_admin.php?userid=".$allUser[$i]->getId()."&action=committee'>Set Committee</a>";
			}else{
				$typeLink = "<a href='user_admin.php?userid=".$allUser[$i]->getId()."&action=regular'>Set Regular</a>";
			}
			
			echo "<tr><td align='center'>".($i+1)."</td>".
				"<td align='center'>[<a href=\"user.php?userid=".$allUser[$i]->getId()."&method=v\">" .$allUser[$i]->getName(). "</a>]</td>".
				"<td align='center'>".$allUser[$i]->getLevel()."</td>".
				"<td align='center'>".$allUser[$i]->getExp()."</td>".
				"<td align='center'>".$allUser[$i]->getRatingA()."</td>".
				"<td align='center'>".$allUser[$i]->getRatingB()."</td>".
				"<td align='center'>[".$enableLink."]</td>".
				"<td align='center'>[".$typeLink."]</td></tr>";
		}
	}else{
	
		echo "<table><th width='10%'>Rank</th><th width='18%'>Name</th><th width='18%'>Level</th><th width='18%'>Exp</th><th width='18%'>Informational Rating</th><th width='18%'>Structural Rating</th>";
		for($i=0;$i<count($allUser);$i++){
			echo "<tr><td align='center'>".($i+1)."</td>".
				"<td align='center'>[<a href=\"user.php?userid=".$allUser[$i]->getId()."&method=v\">" .$allUser[$i]->getName(). "</a>]</td>".
				"<td align='center'>".$allUser[$i]->getLevel()."</td>".
				"<td align='center'>".$allUser[$i]->getExp()."</td>".
				"<td align='center'>".$allUser[$i]->getRatingA()."</td>".
				"<td align='center'>".$allUser[$i]->getRatingB()."</td></tr>";
		}
	}
	echo "</table><br />";
	echo "<a href=\"#\" onclick=\"history.go(-1);\"> Back </a>";
}else{
	echo "<script language=javascript>\n".
			"alert(\"No user in the system.\");\n".
			"history.go(-1);\n".
			"</script>";
}
?>
</body></html>